package com.lebang.controller.api;

import com.lebang.common.MemberLock;
import com.lebang.common.Require;
import com.lebang.domain.VideoCollectionRecord;
import com.lebang.model.RequestUserInfo;
import com.lebang.service.VideoCollectionService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 短视频关注
 * @author xuweijie
 */
@Api(tags = "短视频关注")
@RestController
@RequestMapping("/videocollection")
public class VideoCollectionApi {
    @Autowired
    private VideoCollectionService videoCollectionService;

    /**
     * 用户关注短视频
     *
     * @param request 请求参数
     * @return 关注结果
     */
    @ApiOperation("关注短视频")
    @PostMapping("/collect")
    @Require
    @MemberLock
    public JsonData collect(HttpServletRequest request, @RequestParam(name = "param") String param) {
        RequestUserInfo requestUserInfo = new RequestUserInfo(request);
        if(TextUtil.isEmpty(param)){
            return Callback.fail("204","参数错误");
        }
            VideoCollectionRecord videoCollectionRecord = videoCollectionService.parseToObj(param,requestUserInfo.getMemberId());
            if(videoCollectionRecord==null){
                return Callback.fail("205","转化错误");
            }

            if(videoCollectionService.checkRecordExit(videoCollectionRecord.getMemberId(),videoCollectionRecord.getmAuthorId())){
                return Callback.fail("206","已关注，请勿重复关注");
            }

            if(videoCollectionService.insertVideoCollection(videoCollectionRecord)){
                return Callback.success("200","关注成功");
            }
            return Callback.fail("207","关注失败");
    }


    /**
     * 用户关注列表
     *
     * @param request 请求参数
     * @return 返回关注列表
     */
    @ApiOperation("关注列表")
    @GetMapping("/list")
    @Require
    @MemberLock
    public JsonData list(HttpServletRequest request) {
        RequestUserInfo requestUserInfo = new RequestUserInfo(request);
        return Callback.success("200","查询成功",videoCollectionService.selectByMemberId(requestUserInfo.getMemberId()));
    }


    /**
     * 取消关注
     * @param request 请求参数
     * @param param 视频作者信息
     * @return 取消结果
     */
    @ApiOperation("取消关注")
    @PostMapping("/cancel")
    @Require
    @MemberLock
    public JsonData cancel(HttpServletRequest request, @RequestParam(name = "param") String param){
        RequestUserInfo requestUserInfo = new RequestUserInfo(request);
        if(TextUtil.isEmpty(param)){
            return Callback.fail("204","参数错误");
        }
            VideoCollectionRecord videoCollectionRecord = videoCollectionService.parseToObj(param,requestUserInfo.getMemberId());
            if(videoCollectionRecord==null){
                return Callback.fail("205","转化错误");
            }

            VideoCollectionRecord record =videoCollectionService.selectCollectRecordByMemberIdAndAuthorId(videoCollectionRecord.getMemberId(),videoCollectionRecord.getmAuthorId());
            if(record==null){
                return Callback.fail("206","没有关注该视频作者");
            }
            if(videoCollectionService.deleteById(record.getId())>0){
                return Callback.success("200","取消成功");
            }
            return Callback.fail("207","取消失败");
    }

}
